// 基础ID类型
export type ID = string | number;

// 时间戳类型
export type Timestamp = string | number | Date;

// 状态类型
export type Status =
  | "active"
  | "inactive"
  | "pending"
  | "completed"
  | "failed"
  | "cancelled";

// 优先级类型
export type Priority = "low" | "medium" | "high" | "urgent";

// 可见性类型
export type Visibility = "public" | "private" | "unlisted" | "restricted";

// 权限类型
export type Permission = "read" | "write" | "delete" | "admin" | "owner";

// 角色类型
export type Role = "user" | "moderator" | "admin" | "super_admin";

// 主题类型
export type Theme = "light" | "dark" | "auto";

// 语言类型
export type Language =
  | "zh-CN"
  | "zh-TW"
  | "en-US"
  | "ja-JP"
  | "ko-KR"
  | "fr-FR"
  | "de-DE"
  | "es-ES"
  | "pt-BR"
  | "ru-RU";

// 排序方向类型
export type SortOrder = "asc" | "desc";

// 文件类型
export type FileType =
  | "image"
  | "video"
  | "audio"
  | "document"
  | "archive"
  | "code"
  | "other";

// MIME类型常量
export const MIME_TYPES = {
  // 图片
  IMAGE_JPEG: "image/jpeg",
  IMAGE_PNG: "image/png",
  IMAGE_GIF: "image/gif",
  IMAGE_WEBP: "image/webp",
  IMAGE_SVG: "image/svg+xml",

  // 视频
  VIDEO_MP4: "video/mp4",
  VIDEO_WEBM: "video/webm",
  VIDEO_AVI: "video/avi",
  VIDEO_MOV: "video/quicktime",

  // 音频
  AUDIO_MP3: "audio/mpeg",
  AUDIO_WAV: "audio/wav",
  AUDIO_OGG: "audio/ogg",
  AUDIO_M4A: "audio/mp4",

  // 文档
  TEXT_PLAIN: "text/plain",
  TEXT_HTML: "text/html",
  TEXT_CSS: "text/css",
  TEXT_JAVASCRIPT: "text/javascript",
  TEXT_MARKDOWN: "text/markdown",

  APPLICATION_JSON: "application/json",
  APPLICATION_XML: "application/xml",
  APPLICATION_PDF: "application/pdf",
  APPLICATION_ZIP: "application/zip",

  // Office文档
  DOCX: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  XLSX: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  PPTX: "application/vnd.openxmlformats-officedocument.presentationml.presentation",

  DOC: "application/msword",
  XLS: "application/vnd.ms-excel",
  PPT: "application/vnd.ms-powerpoint",
} as const;

// 文件大小单位
export const FILE_SIZE_UNITS = ["B", "KB", "MB", "GB", "TB", "PB"] as const;
export type FileSizeUnit = (typeof FILE_SIZE_UNITS)[number];

// 时间单位
export const TIME_UNITS = [
  "second",
  "minute",
  "hour",
  "day",
  "week",
  "month",
  "year",
] as const;
export type TimeUnit = (typeof TIME_UNITS)[number];

// 颜色类型
export interface Color {
  hex: string;
  rgb: {
    r: number;
    g: number;
    b: number;
  };
  hsl: {
    h: number;
    s: number;
    l: number;
  };
  alpha?: number;
}

// 坐标类型
export interface Point {
  x: number;
  y: number;
}

// 尺寸类型
export interface Size {
  width: number;
  height: number;
}

// 矩形类型
export interface Rectangle extends Point, Size {}

// 范围类型
export interface Range<T = number> {
  min: T;
  max: T;
}

// 时间范围类型
export interface TimeRange {
  start: Timestamp;
  end: Timestamp;
}

// 分页信息类型
export interface Pagination {
  page: number;
  limit: number;
  total: number;
  totalPages: number;
  hasNext: boolean;
  hasPrev: boolean;
}

// 排序信息类型
export interface Sort {
  field: string;
  order: SortOrder;
}

// 过滤器类型
export interface Filter {
  field: string;
  operator:
    | "eq"
    | "ne"
    | "gt"
    | "gte"
    | "lt"
    | "lte"
    | "in"
    | "nin"
    | "like"
    | "regex";
  value: any;
}

// 搜索选项类型
export interface SearchOptions {
  query?: string;
  filters?: Filter[];
  sort?: Sort[];
  pagination?: Partial<Pagination>;
  facets?: string[];
  highlight?: boolean;
}

// 选项类型
export interface Option<T = any> {
  label: string;
  value: T;
  disabled?: boolean;
  description?: string;
  icon?: string;
  color?: string;
  metadata?: Record<string, any>;
}

// 树节点类型
export interface TreeNode<T = any> {
  id: ID;
  label: string;
  value?: T;
  children?: TreeNode<T>[];
  parent?: TreeNode<T>;
  level: number;
  expanded?: boolean;
  selected?: boolean;
  disabled?: boolean;
  icon?: string;
  metadata?: Record<string, any>;
}

// 菜单项类型
export interface MenuItem {
  id: ID;
  label: string;
  icon?: string;
  path?: string;
  url?: string;
  action?: string;
  children?: MenuItem[];
  disabled?: boolean;
  hidden?: boolean;
  badge?: string | number;
  shortcut?: string;
  metadata?: Record<string, any>;
}

// 标签类型
export interface Tag {
  id: ID;
  name: string;
  color?: string;
  description?: string;
  count?: number;
}

// 徽章类型
export interface Badge {
  text: string;
  type?: "default" | "primary" | "success" | "warning" | "danger" | "info";
  size?: "small" | "medium" | "large";
  variant?: "solid" | "outline" | "ghost";
}

// 进度类型
export interface Progress {
  current: number;
  total: number;
  percentage: number;
  status?: "normal" | "success" | "warning" | "error";
  message?: string;
}

// 统计数据类型
export interface Stat {
  label: string;
  value: number | string;
  change?: number;
  changeType?: "increase" | "decrease" | "neutral";
  format?: "number" | "percentage" | "currency" | "duration" | "bytes";
  unit?: string;
  icon?: string;
  color?: string;
}

// 图表数据点类型
export interface DataPoint {
  x: number | string | Date;
  y: number;
  label?: string;
  color?: string;
  metadata?: Record<string, any>;
}

// 图表系列类型
export interface ChartSeries {
  name: string;
  data: DataPoint[];
  type?: "line" | "bar" | "area" | "pie" | "scatter";
  color?: string;
  visible?: boolean;
}

// 地理位置类型
export interface Location {
  latitude: number;
  longitude: number;
  address?: string;
  city?: string;
  region?: string;
  country?: string;
  postalCode?: string;
}

// 联系信息类型
export interface Contact {
  name?: string;
  email?: string;
  phone?: string;
  address?: string;
  website?: string;
  socialLinks?: Record<string, string>;
}

// 货币类型
export interface Currency {
  code: string; // ISO 4217
  symbol: string;
  name: string;
  decimals: number;
}

// 常用货币
export const CURRENCIES: Record<string, Currency> = {
  USD: { code: "USD", symbol: "$", name: "US Dollar", decimals: 2 },
  EUR: { code: "EUR", symbol: "€", name: "Euro", decimals: 2 },
  GBP: { code: "GBP", symbol: "£", name: "British Pound", decimals: 2 },
  JPY: { code: "JPY", symbol: "¥", name: "Japanese Yen", decimals: 0 },
  CNY: { code: "CNY", symbol: "¥", name: "Chinese Yuan", decimals: 2 },
  KRW: { code: "KRW", symbol: "₩", name: "South Korean Won", decimals: 0 },
};

// 时区类型
export interface Timezone {
  id: string;
  name: string;
  offset: number; // UTC offset in minutes
  abbreviation: string;
}

// 常用时区
export const TIMEZONES: Record<string, Timezone> = {
  UTC: {
    id: "UTC",
    name: "Coordinated Universal Time",
    offset: 0,
    abbreviation: "UTC",
  },
  EST: {
    id: "America/New_York",
    name: "Eastern Standard Time",
    offset: -300,
    abbreviation: "EST",
  },
  PST: {
    id: "America/Los_Angeles",
    name: "Pacific Standard Time",
    offset: -480,
    abbreviation: "PST",
  },
  GMT: {
    id: "Europe/London",
    name: "Greenwich Mean Time",
    offset: 0,
    abbreviation: "GMT",
  },
  CET: {
    id: "Europe/Paris",
    name: "Central European Time",
    offset: 60,
    abbreviation: "CET",
  },
  JST: {
    id: "Asia/Tokyo",
    name: "Japan Standard Time",
    offset: 540,
    abbreviation: "JST",
  },
  CST: {
    id: "Asia/Shanghai",
    name: "China Standard Time",
    offset: 480,
    abbreviation: "CST",
  },
  KST: {
    id: "Asia/Seoul",
    name: "Korea Standard Time",
    offset: 540,
    abbreviation: "KST",
  },
};

// 验证规则类型
export interface ValidationRule {
  type:
    | "required"
    | "min"
    | "max"
    | "minLength"
    | "maxLength"
    | "pattern"
    | "email"
    | "url"
    | "custom";
  value?: any;
  message: string;
  validator?: (value: any) => boolean | Promise<boolean>;
}

// 表单字段类型
export interface FormField {
  name: string;
  label: string;
  type:
    | "text"
    | "email"
    | "password"
    | "number"
    | "tel"
    | "url"
    | "textarea"
    | "select"
    | "multiselect"
    | "checkbox"
    | "radio"
    | "date"
    | "datetime"
    | "time"
    | "file"
    | "color"
    | "range";
  placeholder?: string;
  description?: string;
  required?: boolean;
  disabled?: boolean;
  readonly?: boolean;
  hidden?: boolean;
  defaultValue?: any;
  options?: Option[];
  validation?: ValidationRule[];
  metadata?: Record<string, any>;
}

// 工具类型
export type Partial<T> = {
  [P in keyof T]?: T[P];
};

export type Required<T> = {
  [P in keyof T]-?: T[P];
};

export type Pick<T, K extends keyof T> = {
  [P in K]: T[P];
};

export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export type Record<K extends keyof any, T> = {
  [P in K]: T;
};

export type Nullable<T> = T | null;

export type Optional<T> = T | undefined;

export type DeepPartial<T> = {
  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};

export type DeepRequired<T> = {
  [P in keyof T]-?: T[P] extends object ? DeepRequired<T[P]> : T[P];
};

export type KeyOf<T> = keyof T;

export type ValueOf<T> = T[keyof T];

export type ArrayElement<T> = T extends (infer U)[] ? U : never;

export type PromiseType<T> = T extends Promise<infer U> ? U : never;

export type FunctionType<T> = T extends (...args: any[]) => infer R ? R : never;

// 导出所有类型
export type {
  ID,
  Timestamp,
  Status,
  Priority,
  Visibility,
  Permission,
  Role,
  Theme,
  Language,
  SortOrder,
  FileType,
  FileSizeUnit,
  TimeUnit,
  Color,
  Point,
  Size,
  Rectangle,
  Range,
  TimeRange,
  Pagination,
  Sort,
  Filter,
  SearchOptions,
  Option,
  TreeNode,
  MenuItem,
  Tag,
  Badge,
  Progress,
  Stat,
  DataPoint,
  ChartSeries,
  Location,
  Contact,
  Currency,
  Timezone,
  ValidationRule,
  FormField,
};
